#!/usr/bin/env python

from tags_class import Tags

#{{{ get_id_by_name ( tag_name, id_site, module )
def get_id_by_name ( liwe, tag_name, id_site, module = None ):
	"""
	:INPUT:

		- tag_name:	SAFE_DB_STRING
		- id_site:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
	"""

	t = Tags ( liwe )

	return { 'tag_id': t.get_id_by_name ( tag_name, id_site, module ) }
#}}}
#{{{ get_ids_by_names ( tag_name, id_site, module )
def get_ids_by_names ( liwe, tag_names, id_site, module = None, exp = 0 ):
	"""
	:INPUT:

		- tag_names:	SAFE_DB_STRING
		- id_site:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
		- exp:		SAFE_NUMBER [0]
	"""

	t = Tags ( liwe )

	return { 'tag_ids': t.get_ids_by_names ( tag_names, id_site, module, exp ) }
#}}}
#{{{ get_by_id ( id_tag )
def get_by_id ( liwe, id_tag ):
	"""
	:INPUT:

		- id_tag:	SAFE_NUMBER
	"""

	t = Tags ( liwe )

	return { "tag": t.get_by_id ( id_tag ) }
#}}}
#{{{ search ( tag_names, id_site, module, exp, exclude_tags = '' )
def search ( liwe, tag_names = '', id_site = 0, exp = 0, module = None, exclude_tags = '' ):
	"""
	:INPUT:

		- tag_names:	Tag list separated by "|" SAFE_DB_STRING
		- id_site:	SAFE_NUMBER
		- exp:		SAFE_NUMBER
		- module:	SAFE_DB_STRING
		- exclude_tags: Tag list separated by "|" SAFE_DB_STRING
	"""

	t = Tags ( liwe )
	
	res = t.search ( tag_names = tag_names, id_site = id_site, module = module, exp = exp, exclude_tags = exclude_tags )
	if not res: res = []

	return { "tags": res }
#}}}
#{{{ tags_list_all ( id_site, module, limit = 0 )
def tags_list_all ( liwe, id_site = -1, module = None, limit = 0 ):
	"""
	:INPUT:

		- id_site:	SAFE_NUMBER [-1]
		- module:	SAFE_DB_STRING
		- limit:	SAFE_NUMBER [0]
	"""

	t = Tags ( liwe )

	if id_site == -1:
		id_site = t.site_id

	if id_site == -1:
		return { "tags" : [] }

	res = t.tags_list_all ( id_site, module, limit )
	if not res: res = []

	return { 'tags': res }
#}}}
#{{{ tags_list ( mode, id_obj, id_site, module = None )
def tags_list ( liwe, id_obj, id_site, mode = "all", module = None, id_only = 0, names_only = 0, limit = 0 ):
	"""
	:INPUT:

		- id_obj:	SAFE_DB_STRING
		- mode:		SAFE_DB_STRING
		- id_site:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
		- id_only:	SAFE_BOOL
		- limit:	SAFE_NUMBER
	"""
	t = Tags ( liwe )

	res = t.tags_list ( id_obj, id_site, mode, module, id_only, names_only, limit )
	if not res: res = []

	return { 'tags': res }
#}}}
#{{{ get_relative_tags ( tags, id_site, module )
def get_relative_tags ( liwe, tags, id_site, module ):
	"""
	DESCR:
		Resituisce l'elenco dei tag relativi a TUTTI gli oggetti che hanno TUTTI i tag specificati.

	:INPUT:
		- tags:		SAFE_DB_STRING
		- id_site:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
	"""
	t = Tags ( liwe )

	res = t.get_relative_tags ( tags, id_site, module )
	if not res: res = []

	return { 'tags': res }
#}}}

#===== ADMIN  TAG =====
#{{{ tag_object ( id_obj, id_site, tags, module = None )
def tag_object ( liwe, id_obj, id_site, tags, module = None ):
	"""
	:INPUT:

		- id_obj:	SAFE_DB_STRING
		- id_site:	SAFE_NUMBER
		- tags:		SAFE_DB_STRING
		- module:	SAFE_DB_STRING
	"""

	t = Tags ( liwe )

	t.tag_object ( id_obj, id_site, tags, module )

	return { 'tag': 'OK' }
#}}}
#{{{ del_tag ( tag_name, id_obj, id_site, module )
def del_tag ( liwe, tag_name, id_obj, id_site, module = None ):
	"""
	:INPUT:

		- tag_name:	SAFE_DB_STRING
		- id_site:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
		- id_obj:	SAFE_DB_STRING
	"""

	t = Tags ( liwe )

	t.del_tag ( tag_name, id_obj, id_site, module )

	return { 'tag': 'OK' }
#}}}
#{{{ del_all_tags ( id_obj, id_site, module )
def del_all_tags ( liwe, id_obj, id_site, module = None ):
	"""
	:INPUT:

		- id_site:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
		- id_obj:	SAFE_DB_STRING
	"""

	t = Tags ( liwe )

	t.del_all_tags ( id_obj, id_site, module )

	return { 'tag': 'OK' }
#}}}

#====  USER META ====
#{{{ list_meta_parent ( id_site, module = None )
def list_meta_parent ( liwe, id_site = None, module = None ):
	"""
	:INPUT:
		- id_site	SAFE_NUMBER
		- module:	SAFE_DB_STRING
	"""

	t = Tags ( liwe )

	if not id_site: id_site = t.site_id

	res = t.list_meta_parent ( id_site, module )
	if not res: res = []

	return { 'tags': res }
#}}}
#{{{ list_meta_by_parent ( parent_name, id_site, module = None )
def list_meta_by_parent ( liwe, parent_name, id_site = None, module = None ):
	"""
	:INPUT:

		- id_site:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
	"""

	t = Tags ( liwe )

	if not id_site: id_site = t.site_id

	res = t.list_meta_by_parent ( parent_name, id_site, module )
	if not res: res = []

	return { 'tags': res }
#}}}

#====  ADMIN META ====
#{{{ add_meta ( tag1, tag2, id_site, module = None )
def add_meta ( liwe, tag1, tag2, id_site = None, module = None ):
	"""
	:INPUT:

		- tag1:		SAFE_DB_STRING
		- tag2:		SAFE_DB_STRING
		- id_site:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
	"""

	t = Tags ( liwe )

	if not id_site: id_site = t.site_id

	t.add_meta ( tag1, tag2, id_site, module )

	return { 'tag': 'OK' }
#}}}
#{{{ del_meta ( tag1, tag2, id_site, module = None )
def del_meta ( liwe, tag1, tag2, id_site, module = None ):
	"""
	:INPUT:

		- tag1:		SAFE_DB_STRING
		- tag2:		SAFE_DB_STRING
		- id_site:	SAFE_NUMBER
		- module:	SAFE_DB_STRING
	"""

	t = Tags ( liwe )

	t.del_meta ( tag1, tag2, id_site, module )

	return { 'tag': 'OK' }
#}}}

def list_affinity ( liwe, id_obj, id_tags, id_site, module = None ):
	"""
	:INPUT:
		- id_obj:	Original id_obj to be excluded from result list		SAFE_DB_STRING
		- id_tags:	Original tags for ``id_obj`` (ids are separated bu "|") SAFE_DB_STRING
		- id_site:	ID site to search tags on.				SAFE_NUMBER
		- module:	Site module						SAFE_DB_STRING
	"""

	t = Tags ( liwe )
	obj_tags = id_tags.split ( "|" )
	
	res = t.list_affinity ( id_obj, obj_tags, id_site, module )

	return { 'affinity_list' : res }
